package com.kun.demo02;

import org.junit.Test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestUtils {

    @Test
    public void testInsert() {
        Connection conn =null;
        Statement st = null;
        ResultSet rs =null;
        try {
            conn = Utils.getConnection();//获取连接
            st = conn.createStatement();//获取SQL执行对象
            String sql = "INSERT INTO users(id,`NAME`,`PASSWORD`,`email`,`birthday`)" +
                    "VALUES(5,'sanjin','123456','233223@qq.com','2020-01-01')";

            int i = st.executeUpdate(sql);
            if(i>0){
                System.out.println("插入成功");
            }
            Utils.release(conn,st,rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testSelect() {
        Connection conn =null;
        Statement st = null;
        ResultSet rs =null;
        try {
            conn = Utils.getConnection();//获取连接
            st = conn.createStatement();//获取SQL执行对象
            String sql = "select * from users";
            rs=st.executeQuery(sql);//查询完毕返回结果集

            while (rs.next()){
                System.out.println(rs.getString("NAME"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                Utils.release(conn,st,rs);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
